# def edit_task(task_name, card_frame):
#     # 弹出对话框询问用户输入新的任务名称
#     new_task_name = simpledialog.askstring("Edit task name", f"Current task: {task_name}\nPlease enter new task name")
#     # 如果用户输入了新的任务名称
#     if new_task_name:
#         # 更新任务名称标签
#         for widget in card_frame.winfo_children():
#             if isinstance(widget, tk.Label) and widget.cget('text').startswith("Task:"):
#                 widget.config(text=f"Task: {new_task_name}")
#                 break  # 找到后直接跳出循环，提高效率


from tkinter import messagebox, filedialog
import tkinter as tk
from .save_edits import save_edits_func

from tkcalendar import DateEntry



def edit_task_func(card_frame, current_task_name, current_priority, current_due_date, current_image_path):
    window=tk.Tk()
    edit_dialog = tk.Toplevel(window)
    edit_dialog.title("Edit Task")
    edit_dialog.geometry("400x300")
    
    task_name_var = tk.StringVar(value=current_task_name)
    priority_var = tk.StringVar(value=current_priority)
    due_date_var = tk.StringVar(value=current_due_date)
    image_path_var = tk.StringVar(value=current_image_path)

    task_name_label = tk.Label(edit_dialog, text="Task Name:")
    task_name_label.pack(pady=5)
    task_name_entry = tk.Entry(edit_dialog, textvariable=task_name_var)
    task_name_entry.pack(pady=5)
    
    priority_label = tk.Label(edit_dialog, text="Priority:")
    priority_label.pack(pady=5)
    priority_options = ["High", "Medium", "Low"]
    priority_menu = tk.OptionMenu(edit_dialog, priority_var, *priority_options)
    priority_menu.pack(pady=5)
    
    due_date_label = tk.Label(edit_dialog, text="Due Date:")
    due_date_label.pack(pady=5)
    due_date_entry = DateEntry(edit_dialog, textvariable=due_date_var, date_pattern='yyyy-mm-dd')
    due_date_entry.pack(pady=5)

    def select_image():
        try:
            filename = filedialog.askopenfilename(title="Select Image", filetypes=[("Image Files", "*.png;*.jpg;*.jpeg;*.gif")])
            if filename:
                image_path_var.set(filename)
        except Exception as e:
            messagebox.showerror("wrongly select picture", str(e))

    confirm_button = tk.Button(edit_dialog, text="Confirm", command=lambda: save_edits_func(card_frame, task_name_var.get(), priority_var.get(), due_date_entry.get_date().strftime('%Y-%m-%d'), image_path_var.get(), edit_dialog))
    confirm_button.pack(pady=10)
    select_image_button = tk.Button(edit_dialog, text="Select Image", command=select_image)
    select_image_button.pack(pady=5)
